//
//  MLASavePinCateVC.m
//  MagicLampApp
//
//  Created by Zheng on 2018/12/24.
//  Copyright © 2018 zheng.magic.lamp.app. All rights reserved.
//

#import "MLASavePinCateVC.h"
#import "MLASavePinCateModel.h"

@interface MLASavePinCateVC ()

@end

@implementation MLASavePinCateVC {
    NSString *typeId;
    NSArray *typeIdArr;
    
    NSMutableArray<MLASavePinCateModel *> *modelsArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self configTitle:@"类别选择"];
    
    if ([_type isEqualToString:@"1"]) {
        self.titlesArr = @[@"供应商",@"采购商"];
        typeIdArr = @[@"1",@"2"];
    } else if ([_type isEqualToString:@"2"]) {
        self.titlesArr = @[@"票据",@"凭证"];
        typeIdArr = @[@"1",@"2"];
        [self.tableView reloadData];
    } else if ([_type isEqualToString:@"3"]) {
        [self loadData];
        
    } else {
        [self loadData];
    }
}


- (void)loadData {
    
    NSString *api;
    if ([_type isEqualToString:@"3"]) {
        api = kStorgeRoomList;
    } else {
        api = kSavePinTypeList;
    }
    
    [MLAHttpManage postHostType:MLAHttpHostType_Jinxiaocun api:api params:nil success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        
        self.page++;
        self.haveRefreshFooter = YES;
        NSArray *lists = response[@"lists"];
        self->modelsArr = [NSMutableArray array];
        self->modelsArr = [MLASavePinCateModel mj_objectArrayWithKeyValuesArray:lists];
        if (self->modelsArr.count < 20) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }

        NSMutableArray *arr = [[NSMutableArray alloc] init];
        for (MLASavePinCateModel *model in self->modelsArr) {
            [arr addObject:model.name];
        }
        self.titlesArr = [arr copy];
        [self.tableView reloadData];
        
    } failure:^{
        
    }];
}

- (void)loadMoreData {
    
    NSString *api;
    if ([_type isEqualToString:@"3"]) {
        api = kStorgeRoomList;
    } else {
        api = kSavePinTypeList;
    }

    NSDictionary *params = @{@"page":@(self.page)};
    [MLAHttpManage postHostType:MLAHttpHostType_Jinxiaocun api:api params:params success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        
        self.page++;
        
        if ([response[@"lists"] count] < 20) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        } else {
            [self.tableView.mj_footer endRefreshing];
        }
        
        [self->modelsArr addObjectsFromArray:[MLASavePinCateModel mj_objectArrayWithKeyValuesArray:response[@"lists"]]];
        NSMutableArray *arr = [[NSMutableArray alloc] init];
        for (MLASavePinCateModel *model in self->modelsArr) {
            [arr addObject:model.name];
        }
        self.titlesArr = [arr copy];
        [self.tableView reloadData];
        
    } failure:^{
        
    }];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [super tableView:tableView didSelectRowAtIndexPath:indexPath];
    
    CX_Log(@"选择类别 %ld",indexPath.row);

    if ([_type isEqualToString:@"1"] || [_type isEqualToString:@"2"]) {
        if (_typeSelectBlock) {
            _typeSelectBlock(self.titlesArr[indexPath.row],typeIdArr[indexPath.row]);
        }
    } else {
        if (_typeSelectBlock) {
            MLASavePinCateModel *model = modelsArr[indexPath.row];
            _typeSelectBlock(model.name, model.modelId);
        }
    }

    [self.navigationController popViewControllerAnimated:YES];
}


@end
